﻿var $utility = {
    queryString: {
        getParameterByName: function (name) {
            name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
            var regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
            var results = regex.exec(location.search);
            return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
        }
    },

    currency: {
        translateToCapital: function (value) {
            var units = ["", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟"];
            var numbers = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
            var text = value.toString();
            var result = "元正";
            var zero;
            for (var i = 0; i < text.length; i++) {
                var digit = parseInt(text.charAt(text.length - 1 - i));
                var unit = units[i];
                var number = numbers[digit];
                var carry = ((i % 4) === 0);
                if (digit === 0) {
                    if (carry) {
                        result = (unit + result);
                    } else if (!zero) {
                        result = (number + result);
                    }
                } else {
                    result = (number + unit + result);
                }
                zero = (digit === 0);
            }
            return result;
        }
    },

    date: {
        getFormat: function () {
            return "yyyy-m-d";
        },

        toLocalDateString: function (date) {
            var format = $utility.date.getFormat();
            return format.replace("yyyy", date.getFullYear().toString())
                .replace("m", (date.getMonth() + 1).toString())
                .replace("d", date.getDate().toString());
        }
    }
};

var $security = {
    roleAdministrator: "Administrator",
    roleManager: "Manager",
    roleEmployee: "Employee"
};